package com.moliku.insurance.common.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
/**
 * @ClassName 赵项阳
 * @Description: TODO
 * @Author DELL
 * @Date 2021/3/1
 * @Version V1.0
 **/
public class AesUtil {

    private final static String CHARSET = "UTF-8";
    private final static String KeySecter="4BC69F15A8740F1F4F5C775F5EEB7B6D";

    // 加密过程
    public static String encryptBase64(String seed, String content) {
        try {
            byte[] rawKey = getRawKey(seed);
            SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] result = cipher.doFinal( content.getBytes(CHARSET));
            return Base64.encodeBase64String(result);
        } catch (Exception e) {
            System.out.println("AES加密base64编码出错");
            throw new RuntimeException(e);
        }
    }
    // 加密过程
    public static String encryptBase64( String content) {
        try {
            byte[] rawKey = getRawKey(KeySecter);
            SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] result = cipher.doFinal( content.getBytes(CHARSET));
            return Base64.encodeBase64String(result);
        } catch (Exception e) {
            System.out.println("AES加密base64编码出错");
            throw new RuntimeException(e);
        }
    }

    //解密过程
    public static String decryptBase64(String seed, String encrypted) {
        try {
            byte[] rawKey = getRawKey(seed);
            byte[] enc = Base64.decodeBase64(encrypted);

            SecretKeySpec skeySpec = new SecretKeySpec(rawKey, "AES");
            Cipher cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] result = cipher.doFinal(enc);
            return new String(result, CHARSET);
        } catch (Exception e) {
            System.out.println("AES解密base64出错");
            throw new RuntimeException(e);
        }
    }

    //转换数据类型
    //转换数据类型
    private static byte[] getRawKey(String seed) throws NoSuchAlgorithmException, NoSuchProviderException {
        int len = seed.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = Integer.valueOf(seed.substring(2 * i, 2 * i + 2), 16).byteValue();
        }
        return  result;
    }

    public static void main(String[] args) throws Exception {

        //密钥--由保司提供32位密鈅
        String seed = "4BC69F15A8740F1F4F5C775F5EEB7B6D";

        // 需要加密的文本
        String content = "<?xml version=\"1.0\" encoding=\"GB2312\" standalone=\"yes\"?>\n" +
                "<ApplyInfo>\n" +
                "    <GeneralInfo>\n" +
                "        <Md5Value>cskhudsjm,dcuhk</Md5Value>\n" +
                "        <PlateformCode>cs</PlateformCode>\n" +
                "        <UUID>524562045</UUID>\n" +
                "    </GeneralInfo>\n" +
                "    <PolicyInfos>\n" +
                "        <PolicyInfo>\n" +
                "            <Applicant>\n" +
                "                <appliAddress>浣樺\uE18D涔拌溅瀹夋叞</appliAddress>\n" +
                "                <appliIdEmail>9648596461</appliIdEmail>\n" +
                "                <appliIdMobile>63521865</appliIdMobile>\n" +
                "                <appliIdNo>63463651</appliIdNo>\n" +
                "                <appliIdType>鎵嶅紑濮?/appliIdType>\n" +
                "                <appliIdentity>鍒濊瘯鎴愮哗鍗栧\uE63F灏戦厭寮€杞﹂棬钀?/appliIdentity>\n" +
                "                <appliName>鏄ヨ妭鍚庢矙</appliName>\n" +
                "                <appliSex>鎵嶅紑濮嬬殑</appliSex>\n" +
                "                <sendSMS>286456825</sendSMS>\n" +
                "            </Applicant>\n" +
                "            <endDate>2020-03-15</endDate>\n" +
                "            <endHour>24</endHour>\n" +
                "            <Insureds>\n" +
                "                <Insured>\n" +
                "                    <insuredIdType>鏀堕\uE635鍏峰拰鎶€鏈?/insuredIdType>\n" +
                "                    <insuredIdentity>涓夊ぇ鍩庡競鐨勬祴璇?/insuredIdentity>\n" +
                "                    <insuredName>鍞变粈涔堟瓕閮戒細绮剧\uE6A3</insuredName>\n" +
                "                    <insuredSeqNo>852</insuredSeqNo>\n" +
                "                </Insured>\n" +
                "                <Insured>\n" +
                "                    <insuredIdType>浜岄亾婀惧\uE179姘斿晩</insuredIdType>\n" +
                "                    <insuredIdentity>涓嬫\uE0BC鑻︽订</insuredIdentity>\n" +
                "                    <insuredName>闃垮痉璺?/insuredName>\n" +
                "                    <insuredSeqNo>12284</insuredSeqNo>\n" +
                "                </Insured>\n" +
                "            </Insureds>\n" +
                "            <InsuredPlan>\n" +
                "                <rationType>45346</rationType>\n" +
                "            </InsuredPlan>\n" +
                "            <Linker>\n" +
                "                <linkerName>鏄\uE21B祴閫?/linkerName>\n" +
                "                <linkerPhone>.168637845</linkerPhone>\n" +
                "            </Linker>\n" +
                "            <operateTimes>6346639</operateTimes>\n" +
                "            <riskCode>2</riskCode>\n" +
                "            <SerialNo>5415216</SerialNo>\n" +
                "            <startDate>2020-02-12</startDate>\n" +
                "            <startHour>0</startHour>\n" +
                "            <sumAmount>56563</sumAmount>\n" +
                "            <sumPremium>2265</sumPremium>\n" +
                "        </PolicyInfo>\n" +
                "    </PolicyInfos>\n" +
                "</ApplyInfo>\n";
        System.out.println("加密前============》" + content);
        // 加密
        String enString = encryptBase64( content);
        System.out.println("加密前============》" + enString);

        // 解密
        String DeString = decryptBase64(seed, enString);
        System.out.println("解密后============》" + DeString);
    }
}
